package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.OrderCourseWebVo;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.excel.OneSubject;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.eduservice.service.client.OrderClient;
import com.atguigu.eduservice.service.client.UcenterClient;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author He Wentao
 * @since 2022/9/24 22:22
 */
@RestController
@RequestMapping("/eduservice/")
public class CourseFrontController {

    @Resource
    private EduCourseService courseService;

    @Resource
    private EduSubjectService subjectService;

    @Resource
    private EduChapterService chapterService;

    @Resource
    private UcenterClient ucenterClient;

    @Resource
    private OrderClient orderClient;

    // 查询课程详情的 章节、小结信息
    @GetMapping("coursefront/getFrontCourseInfo/{id}")
    public R getCourseInfo(@PathVariable("id") String courseId, HttpServletRequest request) {

        // 获取拼接好的 章节、小结 信息，这里修改返回结果 R 为 list 了！！！
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        // 使用自定义 SQL 查询需要的课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        String token = request.getHeader("token");
        Map<String, Object> data = ucenterClient.getInfoByToken(token).getData();

        // 获取到用户 id
        String memberId = (String) data.get("memberId");
        assert memberId!=null;

        // 远程调用
        boolean buyCourse = orderClient.isBuyCourse(courseId, memberId);


        return R.ok().data("courseWebVo", courseWebVo)
                .data("chapterVideoList", chapterVideoList)
                .data("buyCourse",buyCourse);
    }


    // 分页查询 带条件
    @PostMapping("/coursefront/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable("page") long page, @PathVariable("limit") long limit,
                                @RequestBody(required = false) CourseFrontVo vo) {

        Page<EduCourse> coursePage = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(coursePage, vo);

        return R.ok().data(map);
    }

    // 查询课程列表中，需要的的一级分类 和对应的 二级分类
    @GetMapping("/subject/front/getAllSubject")
    public R getSubjectInfo() {
        List<OneSubject> list = subjectService.getAllOneTwoSubject();

        return R.ok().data("list", list);

    }


    //  根据课程 id 查询课程信息，并且返回对应的 VO 对象
    @GetMapping("/coursefront/getCourseById/{id}")
    public OrderCourseWebVo getCourseById(@PathVariable("id") String id) {
        // 使用自定义 SQL 查询需要的课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);

        OrderCourseWebVo orderCourseWebVo = new OrderCourseWebVo();

        BeanUtils.copyProperties(courseWebVo, orderCourseWebVo);

        return orderCourseWebVo;
    }

}
